<?php
require_once ('dbconfig.php');
require_once ('igb.php');

?>
<html>
<head>
<title>Corporate Ore Payout Calculator</title>
</head>
<body>


<?php
$oreNames = $_POST ['oreN'];
$orePrices = $_POST ['oreP'];
//$oreQtys = $_POST ['oreQ'];
$sendRC = $_POST ['showsend'];
$receiptMsg = "";

$dbh = mysql_connect ( DB_HOST, DB_USER, DB_PASS ) or die ( mysql_error () );
mysql_select_db ( DB_NAME );
$sql = "select mbrName from CorpMembers order by mbrName";

if ($sendRC == "") {
	echo "<table border=1><tr><td><b>Ore</b></td><td align=\"right\"><b>Price</b></td><td><b>Qty</b></td><td><b>Value</b></td></tr>\n";
	$i = 0;
	$payOut = 0;
	while ( $i <= 59 ) {
		$oreQtyName = "oreQ" . $i;
		$oreQtys = $_POST [$oreQtyName];
		if ($oreQtys != "") {
			$oreValue = $oreQtys * $orePrices [$i];
			echo "<tr><td align=\"left\">$oreNames[$i]</td>";
			echo "<td align=\"right\">$orePrices[$i]</td>";
			echo "<td align=\"right\">$oreQtys</td><td align=\"right\">" . number_format ( $oreValue, 2 ) . "</td></tr>\n";
			$payOut += $oreValue;
			
			$receiptMsg .= "Ore: " . $oreNames [$i] . " " . $oreQtys . " @ " . $orePrices [$i] . " = " . number_format ( $oreValue, 2 ) . "\n";
		}
		$i ++;
	} //end while loop
	$payOutValue = number_format ( $payOut, 2 );
	$receiptMsg .= "\n\n";
	$receiptMsg .= "Total: " . number_format ( $payOut, 2 );
	echo "<tr><td></td><td></td><td></td><td>$payOutValue</td></tr>\n";
	echo "</table>\n";
	
	$result = mysql_query ( $sql, $dbh ) or die ( mysql_error () );
	
	echo "<br><br>Build Receipt for: <form action=\"testCalc2.php\" method=\"POST\">
<br>
<select name=\"member\"><br>";
	
	while ( $newArray = mysql_fetch_array ( $result ) ) {
		$rcptMember = $newArray ['mbrName'];
		echo "<option value=\"$rcptMember\">$rcptMember</option>";
	
	} //end of while loop
	echo "</select>
<input type=\"hidden\" name=\"msg\" value=\"" . $receiptMsg . "\"><br>
<input type=\"hidden\" name=\"showsend\" value=\"send\">
<input type=\"submit\" name=\"submit\" value=\"Build Receipt\">
</form>";
}

if ($sendRC == "send") {
	
	echo $_POST ['msg'];
	echo "<br><br>";
	$receiptLink = "<a href=\"evemailto:" . $_POST ['member'] . ", Mograthi, Veeja\" SUBJECT=\"Ore Payment Receipt for " . $_POST ['member'] . "\" message=\"" . $_POST ['msg'] . "\">Send Receipt</a>";
	echo $receiptLink;
	echo "<br><br><a href=\"testCalc.php\">Back</a>";
}

?> 
</body>
</html>
